<template>
    <view class="content my">
	    <view class="userDiv">
			<uni-list>
				<uni-list-chat 
					:avatar="$getImg(userPhoto, 'head')" 
					:title="userName" 
					:note="orgName" 
					:desc="postName"
					clickable
					to="/pages/platform/my/user"
				>
				</uni-list-chat>
			</uni-list>
		</view>
        <view>
           <uni-list>
				<uni-list-item :show-extra-icon="true" :extra-icon="cacheIcon" :title="$t('app.my.clearCache')" clickable @click="clearCache"></uni-list-item>
				<uni-list-item :show-extra-icon="true" :extra-icon="pwdIcon" :title="$t('app.my.changePwd')" clickable @click="updatePwd"></uni-list-item>
				<uni-list-item :show-extra-icon="true" :extra-icon="languageIcon" :title="$t('app.my.language')" clickable @click="language" v-if="openI18n"></uni-list-item>
				<uni-list-item :show-extra-icon="true" :extra-icon="codeIcon" :title="$t('app.my.dataDic')" clickable @click="code" v-if="openOffline"></uni-list-item>
				<uni-list-item :show-extra-icon="true" :extra-icon="aboutIcon" :title="$t('app.my.about')" clickable @click="about"></uni-list-item>
				<uni-list-item :show-extra-icon="true" :extra-icon="helpIcon" :title="$t('app.my.help')" clickable @click="help"></uni-list-item>
				<uni-list-item :show-extra-icon="true" :extra-icon="opinionIcon" :title="$t('app.my.feedback')" clickable @click="feedback"></uni-list-item>
            </uni-list>
        </view>
        <view class="btnDiv">
            <button class="landing" type="warn" @click="logout">{{$t('app.my.logout')}}</button>
        </view>
    </view>
</template>

<script>
	import { business, utilities } from '@/utils/common.js'
	import { getUserInfoByName, logout } from '@/api/platform/oauth.js'
	
	export default {
		data() {
			return {
				userPhoto: '',
				orgName: '',
				postName: '',
				cacheIcon: {
					color: '#1E91E6',
					size: '22',
					type: 'trash'
				},
				pwdIcon: {
					color: '#1E91E6',
					size: '22',
					type: 'locked'
				},
				languageIcon: {
					color: '#1E91E6',
					size: '22',
					type: 'chat'
				},
				codeIcon: {
					color: '#1E91E6',
					size: '22',
					type: 'wallet'
				},
				aboutIcon: {
					color: '#1E91E6',
					size: '22',
					type: 'info'
				},
				helpIcon: {
					color: '#1E91E6',
					size: '22',
					type: 'help'
				},
				opinionIcon: {
					color: '#1E91E6',
					size: '22',
					type: 'compose'
				},
				backButtonPress: 0
			}
		},
		computed: {
			userName() {
				return uni.getStorageSync("userName")
			},
			openI18n() {
				return utilities.getConfig('PROJECT').OPEN_I18N
			},
			openOffline() {
				return utilities.getConfig('PROJECT').OPEN_OFFLINE
			}
		},
		onLoad() {
			this.init()
		},
		onShow() {	
			business.refreshMsgCount()
		},
		methods: {
			init() {
				let account = uni.getStorageSync("userAccount")
				getUserInfoByName(account).then(res=>{
					this.userPhoto = res.data.user.photo
					this.orgName = res.data.org.name
					const posts = []
					if(res.data.positions){
						res.data.positions.forEach(item=>{
							posts.push(item.name)
						})
					}
					this.postName = posts.join()
				})
			},
			viewUser() {
				uni.navigateTo({
					url: '/pages/platform/my/user'
				})
			},
			logout(){
				uni.showModal({
					title: this.$t('baseCommon.message.tips'),
					content: this.$t('app.message.sureLogout'),
					success: function (res) {
						if (res.confirm) {
							logout().then(()=>{
								// 清除token
								uni.removeStorageSync('_app_token');
								// 清除传感器
								uni.removeStorageSync('bindSensor');
							})
							uni.reLaunch({
								url: '/pages/platform/login/index'
							});
						}
					}
				})
			},
			clearCache() {
				uni.showModal({
					title: this.$t('baseCommon.message.tips'),
					content: this.$t('app.message.sureClearCache'),
					success: function (res) {
						if (res.confirm) {
							uni.clearStorage()
							setTimeout(function(){
								uni.reLaunch({
									url: '/pages/platform/login/index'
								});
							},2000)
						}
					}
				})
			},
			updatePwd(){
				uni.navigateTo({
					url: '/pages/platform/my/password'
				})
			},
			language() {
				uni.navigateTo({
					url: '/pages/platform/my/language'
				})
			},
			code() {
				uni.navigateTo({
					url: '/pages/platform/my/code'
				})
			},
			about() {
				uni.navigateTo({
					url: '/pages/platform/my/about'
				})
			},
			help() {
				
			},
			feedback() {
				
			}
		},
		onBackPress() {
			this.backButtonPress++;
			if (this.backButtonPress > 1) { 
				// 退出清除角标
				plus.runtime.setBadgeNumber(0);
				// 退出清除token
				uni.removeStorageSync('_app_token');
				plus.runtime.quit();
			} else {
				plus.nativeUI.toast(this.$t('app.message.pressAgainExit'));
			}
			return true;
		}
	}
</script>

<style lang="scss">
	.userDiv {
		background-color: #F4F5F6;
		padding: 10px 0; 
	}
	
	.uni-list {
		border-bottom: 1px solid #F0F0F0;
	}
	
	.btnDiv {
		padding: 5px 10px;
		margin-top: 20px;
	}
</style>
